package com.ayi.dao.bookdao;

import com.ayi.bean.Book;
import com.ayi.utils.GetConn;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @author Ayingzz
 * @create 2021/12/5 12:42
 */
public class AddBook {

    /**
     * 添加一本书
     * @param book
     * @return
     */
    public boolean addBook(Book book) {
        Connection conn = new GetConn().getConnection();
        String sql = "insert into Books(name, author, type, details, price, inventory, imgPath) values(?, ?, ?, ?, ?, ?, ?)";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, book.getName());
            ps.setString(2, book.getAuthor());
            ps.setString(3, book.getType());
            ps.setString(4, book.getDetails());
            ps.setDouble(5, book.getPrice());
            ps.setInt(6, book.getInventory());
            ps.setString(7, book.getImgPath());
            ps.execute();
            return true;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return false;
    }


}
